//
//  InfoTemplate2.m
//  OA
//
//  Created by apple on 15-12-2.
//  Copyright (c) 2015年 net. All rights reserved.
//

#import "InfoTemplate2.h"
#import "AppDelegate.h"
#import "Constants.h"
#import "Template2AttachContent.h"

@implementation InfoTemplate2

NSArray * info;
AppDelegate * appDelegate;
int result2=-1;
NSString * finalDocumentName2;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"信息内容";
    appDelegate = [UIApplication sharedApplication].delegate;
    NSDictionary * params = @{@"id":self.infoid};
    NSString *rar = @"rar";
    NSString *zip = @"zip";
    [appDelegate.manager
     POST:url6
     parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         info = responseObject;
         NSDictionary * dict = [info objectAtIndex:0];
         self.label1.text = [dict objectForKey:@"title"];
//         if ([[dict objectForKey:@"attachment"] length]>0) {
//             self.label2.text=@"下载附件";
//         }else{
//             self.label2.text=@"";
//         }
         NSString * attachmentName = [dict objectForKey:@"attachment"];
         NSString * documentName = [attachmentName substringToIndex:14];
         //NSLog(@"documentName:%@",documentName);
         NSRange rangerar = [attachmentName rangeOfString:rar];
         NSRange rangezip = [attachmentName rangeOfString:zip];
         NSUInteger locationrar = rangerar.location;
         NSUInteger leightrar = rangerar.length;
         NSUInteger locationzip = rangezip.location;
         NSUInteger leightzip = rangezip.length;
         NSString *astringrar = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%lu,Leight:%lu",(unsigned long)locationrar,(unsigned long)leightrar]];
         //NSLog(@"astringrar:%@",astringrar);
         NSString *astringzip = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%lu,Leight:%lu",(unsigned long)locationzip,(unsigned long)leightzip]];
         //NSLog(@"astringzip:%@",astringzip);
         if ([attachmentName length]>0){
             if (leightzip==3L) {
                 [self.btn2 setTitle:@"下载附件" forState:UIControlStateNormal];
                 [self.btn1 setTitle:@"" forState:UIControlStateNormal];
                 finalDocumentName2 = [documentName stringByAppendingString:@".zip"];
                 result2=1;
                 //NSLog(@"finalDocumentName:%@",finalDocumentName);
             }else if(leightrar==3L){
                 result2=2;
                 UIAlertView * alert = [[UIAlertView alloc]
                                        initWithTitle:@"提示"
                                        message:@"不支持rar附件"
                                        delegate:nil
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil];
                 [alert show];
                 
             }else{
                 [self.btn1 setTitle:@"下载附件" forState:UIControlStateNormal];
                 [self.btn2 setTitle:@"" forState:UIControlStateNormal];
                 finalDocumentName2 = [documentName stringByAppendingString:[attachmentName substringFromIndex:[attachmentName rangeOfString:@"."].location]];
                 //NSLog(@"finalDocumentName:%@",finalDocumentName);
             }
         }else{
             [self.btn1 setTitle:@"" forState:UIControlStateNormal];
             [self.btn2 setTitle:@"" forState:UIControlStateNormal];
             
         }

         [self.webView loadHTMLString:[dict objectForKey:@"content"] baseURL:nil];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"他妈的异常:%@",error);
     }];
    
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if (result2==1) {
//        AttachList * attachList = (AttachList*)segue.destinationViewController;
//        [attachList setValue:finalDocumentName forKey:@"finalDocumentName"];
    }else if(result2==-1){
        Template2AttachContent * template2AttachContent = (Template2AttachContent*)segue.destinationViewController;
        //NSLog(@"finalDocumentName:%@",finalDocumentName);
        [template2AttachContent setValue:finalDocumentName2 forKey:@"finalDocumentName2"];
    }

}
@end
